package com.ruoyi.factory.erp.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factory.erp.domain.CusMaterial;
import com.ruoyi.factory.erp.domain.PayBillVO;
import com.ruoyi.factory.erp.domain.ZjPayBillVO;
import com.ruoyi.factory.erp.mapper.CusMaterialMapper;
import com.ruoyi.factory.erp.mapper.PayBillMapper;
import com.ruoyi.factory.erp.mapper.ZjPayBillMapper;
import com.ruoyi.factory.erp.service.ErpHandleService;
import com.ruoyi.factory.estimate.utils.SignHelper;
import com.ruoyi.factory.qylock.utils.OkHttpUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ErpHandleServiceImpl
 * @Description: TODO erp信息处理
 * @Author LiuJiaji
 * @Date 2023/10/8 10:03
 **/
@Service
public class ErpHandleServiceImpl implements ErpHandleService {
    protected final Logger logger = LoggerFactory.getLogger(ErpHandleServiceImpl.class);

    @Autowired
    private CusMaterialMapper cusMaterialMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private ZjPayBillMapper zjPayBillMapper;

    /**
     * @author: Liujiaji
     * @date: 2023/10/8 10:07
     * @description: TODO 客户规格同步
     * @param: []
     * @return: void
     **/
    @Override
    public void ggtocode() {
        List<CusMaterial> cusMaterials = cusMaterialMapper.selectAllByCode();
        for (CusMaterial cusMaterial : cusMaterials) {
            cusMaterialMapper.updateCodebyMate(cusMaterial);
        }
    }

    @Override
    public void fkdpushmessage() {

        //查询近两天未支付的
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,-1);
        Date time = instance.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(time);
        PayBillVO payBillVO = new PayBillVO();
        payBillVO.setEffectstatus("10");
        payBillVO.setPaydate(format);
//        payBillVO.setPaydate("2025-07-24");
        List<PayBillVO> payBillVOS = payBillMapper.selectPayBillun(payBillVO);
        for (PayBillVO billVO : payBillVOS) {
            //请求友空间获取个人信息
            JSONObject perparam = new JSONObject();
            perparam.put("pageIndex",1);
            perparam.put("pageSize",10);
            perparam.put("code",billVO.getBillmakercode());
            String fkperres = reqykjutil(perparam.toString(), "/digitalModel/staff/list", "付款单支付完成通知请求友空间人员列表信息");
            JSONObject fkperresrej = JSONObject.fromObject(fkperres);
            String fktzco = fkperresrej.optString("code");
            if(!"200".equals(fktzco)) continue;
            JSONObject jsonObject = fkperresrej.optJSONObject("data");
            JSONArray jsonArray1 = jsonObject.optJSONArray("recordList");
            String userId = "";
            for (Object o : jsonArray1) {
                JSONObject jsonObject1 = JSONObject.fromObject(o);
                userId = jsonObject1.optString("user_id");
            }
            if(StringUtils.isBlank(userId)) continue;

            JSONObject paramj = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(userId);
//            jsonArray.add("a36bef1d-d908-4b80-a907-df329c84deca");
//                String uuid = IdGen.uuid();
            paramj.put("srcMsgId","erpfkdzfzt"+System.currentTimeMillis()+payBillVO.getPaybillpk()+payBillVO.getBillmakercode());
            paramj.put("yhtUserIds",jsonArray);
            paramj.put("title","消息通知");
            String cont = billVO.getBillmakername()+"提交的"+billVO.getSuppliername()+"，付款单"+billVO.getBillno()+"，金额"+billVO.getCurrtypename()+billVO.getMoney()+"，已支付";
            paramj.put("content",cont);
            String res = reqykjutil(paramj.toString(), "/uspace/rest/openapi/idempotent/work/notify/push", "付款单支付完成通知请求友空间消息通知");
            JSONObject jsonObject1 = JSONObject.fromObject(res);
            String s = jsonObject1.optString("code");
            if("200".equals(s)){
                JSONObject jsonObject2 = jsonObject1.optJSONObject("data");
                String s1 = jsonObject2.optString("flag");
                if("0".equals(s1)){
                    //成功
                    billVO.setPushstatus("Y");
                    payBillMapper.updatePayBillpushstatus(billVO);
                }
            }
        }
        //更改推送状态

    }

    @Override
    public void zjfkdpushmessage() {

        //查询近两天未支付的
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,-1);
        Date time = instance.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(time);
        ZjPayBillVO payBillVO = new ZjPayBillVO();
        payBillVO.setEffectstatus("10");
        payBillVO.setPaydate(format);
//        payBillVO.setPaydate("2025-07-24");
        List<ZjPayBillVO> payBillVOS = zjPayBillMapper.selectZjPayBillun(payBillVO);
        for (ZjPayBillVO billVO : payBillVOS) {
            //请求友空间获取个人信息
            JSONObject perparam = new JSONObject();
            perparam.put("pageIndex",1);
            perparam.put("pageSize",10);
            perparam.put("code",billVO.getBillmakercode());
            String fkperres = reqykjutil(perparam.toString(), "/digitalModel/staff/list", "资金付款单支付完成通知请求友空间人员列表信息");
            JSONObject fkperresrej = JSONObject.fromObject(fkperres);
            String fktzco = fkperresrej.optString("code");
            if(!"200".equals(fktzco)) continue;
            JSONObject jsonObject = fkperresrej.optJSONObject("data");
            JSONArray jsonArray1 = jsonObject.optJSONArray("recordList");
            String userId = "";
            for (Object o : jsonArray1) {
                JSONObject jsonObject1 = JSONObject.fromObject(o);
                userId = jsonObject1.optString("user_id");
            }
            if(StringUtils.isBlank(userId)) continue;

            JSONObject paramj = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(userId);
//            jsonArray.add("a36bef1d-d908-4b80-a907-df329c84deca");
//                String uuid = IdGen.uuid();6.095.98
            paramj.put("srcMsgId","erpfkdzfzt"+System.currentTimeMillis()+billVO.getPaybillpk()+billVO.getBillmakercode());
            paramj.put("yhtUserIds",jsonArray);
            paramj.put("title","消息通知");
            if(StringUtils.isNotBlank(billVO.getSuppliername())){
                String cont = billVO.getBillmakername()+"提交的"+billVO.getSuppliername()+"，资金付款单"+billVO.getBillno()+"，金额"+billVO.getCurrtypename()+billVO.getMoney()+"，已支付";
                paramj.put("content",cont);
            }else{
                String cont = billVO.getBillmakername()+"提交的资金付款单"+billVO.getBillno()+"，金额"+billVO.getCurrtypename()+billVO.getMoney()+"，已支付";
                paramj.put("content",cont);
            }

            String res = reqykjutil(paramj.toString(), "/uspace/rest/openapi/idempotent/work/notify/push", "资金付款单支付完成通知请求友空间消息通知");
            JSONObject jsonObject1 = JSONObject.fromObject(res);
            String s = jsonObject1.optString("code");
            if("200".equals(s)){
                JSONObject jsonObject2 = jsonObject1.optJSONObject("data");
                String s1 = jsonObject2.optString("flag");
                if("0".equals(s1)){
                    //成功
                    billVO.setPushstatus("Y");
                    zjPayBillMapper.updateZjPayBillpushstatus(billVO);
                }
            }
        }

    }

    public String reqykjutil (String param, String purl, String desc){
        //推送到友空间
        //使用评价管理的配置
        String token = SignHelper.getToken();
        if(StringUtils.isEmpty(token)) return null;
        String url = "https://c2.yonyoucloud.com/iuap-api-gateway/yonbip"+purl;
        url = url+ "?access_token="+token;
        logger.info(desc+"入参: "+param);
        String s1 = OkHttpUtils.okHttpPost(url, param);
        logger.info(desc+"返参: "+s1);
        return s1;
    }



}
